| Conditions | 8 |
| Total Lines | 163 |
| Code Lines | 131 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import React from "react"; |
||
| 20 | }; |
||
| 21 | |||
| 22 | export default function EmailForm({setToken, navigation, setIsLoggedIn}) { |
||
| 23 | const [name, setName] = useState(null); |
||
| 24 | const [email, setEmail] = useState(null); |
||
| 25 | const [password, setPassword] = useState(null); |
||
| 26 | const [phonenumber, setPhonenumber] = useState(null); |
||
| 27 | const [accept, setAccepts] = useState(false); |
||
| 28 | const [modalVisible, setModalVisible] = useState(false); |
||
| 29 | const [termsVisible, setTermsVisible] = useState(false); |
||
| 30 | |||
| 31 | async function createUser() { |
||
| 32 | |||
| 33 | const userName = name.split(' '); |
||
| 34 | const firstName = userName[0]; |
||
| 35 | const lastName = userName[1]; |
||
| 36 | |||
| 37 | // Check if email is valid |
||
| 38 | if (!(checkEmail(email))) { |
||
| 39 | showMessage({ |
||
| 40 | message: 'Invalid email', |
||
| 41 | type: 'danger', |
||
| 42 | position: 'bottom' |
||
| 43 | }) |
||
| 44 | } else { |
||
| 45 | |||
| 46 | // Prepare user object |
||
| 47 | const user = { |
||
| 48 | firstName: firstName, |
||
| 49 | lastName: lastName, |
||
| 50 | email: email, |
||
| 51 | password: password, |
||
| 52 | phoneNumber: phonenumber, |
||
| 53 | balance: 1.2 |
||
| 54 | }; |
||
| 55 | |||
| 56 | // Register user |
||
| 57 | const result = await authModel.register(user); |
||
| 58 | |||
| 59 | // Check if registration successful |
||
| 60 | if (result['type'] === "success") { |
||
| 61 | logIn(); |
||
| 62 | } |
||
| 63 | |||
| 64 | showMessage({ |
||
| 65 | message: result['title'], |
||
| 66 | description: result['message'], |
||
| 67 | type: result['type'], |
||
| 68 | position: 'bottom' |
||
| 69 | |||
| 70 | }); |
||
| 71 | |||
| 72 | |||
| 73 | }; |
||
| 74 | }; |
||
| 75 | |||
| 76 | async function logIn() { |
||
| 77 | const userLogin = { |
||
| 78 | email: email, |
||
| 79 | password: password |
||
| 80 | }; |
||
| 81 | |||
| 82 | const loginUser = await authModel.login(userLogin); |
||
| 83 | if (Object.prototype.hasOwnProperty.call(loginUser, 'errors')) { |
||
| 84 | showMessage({ |
||
| 85 | message: loginUser['errors']['title'], |
||
| 86 | type: 'danger', |
||
| 87 | position: 'bottom' |
||
| 88 | }) |
||
| 89 | } else { |
||
| 90 | setToken(loginUser['token']); |
||
| 91 | setIsLoggedIn(true); |
||
| 92 | } |
||
| 93 | }; |
||
| 94 | |||
| 95 | |||
| 96 | return ( |
||
| 97 | <View style={styles.container}> |
||
| 98 | <Image style={styles.logo} source={require('../../assets/logo_dark.png')}></Image> |
||
| 99 | <Text style={styles.formTitle}>Register</Text> |
||
| 100 | |||
| 101 | <TextInput |
||
| 102 | placeholder="Firstname Lastname" |
||
| 103 | style={styles.input} |
||
| 104 | onChangeText={(content: string) => { |
||
| 105 | setName(content) |
||
| 106 | }} |
||
| 107 | /> |
||
| 108 | |||
| 109 | <TextInput |
||
| 110 | placeholder="Email" |
||
| 111 | style={styles.input} |
||
| 112 | keyboardType="email-address" |
||
| 113 | onChangeText={(content: string) => { |
||
| 114 | setEmail(content); |
||
| 115 | }} |
||
| 116 | /> |
||
| 117 | |||
| 118 | <TextInput |
||
| 119 | placeholder="Password" |
||
| 120 | style={styles.input} |
||
| 121 | keyboardType='numbers-and-punctuation' |
||
| 122 | secureTextEntry={true} |
||
| 123 | onChangeText={(content: string) => { |
||
| 124 | setPassword(content) |
||
| 125 | }} |
||
| 126 | /> |
||
| 127 | |||
| 128 | <TextInput |
||
| 129 | placeholder="Phonenumber" |
||
| 130 | style={styles.input} |
||
| 131 | keyboardType='phone-pad' |
||
| 132 | onChangeText={(content: string) => { |
||
| 133 | setPhonenumber(content) |
||
| 134 | }} |
||
| 135 | /> |
||
| 136 | |||
| 137 | |||
| 138 | <View style={styles.termsContainer}> |
||
| 139 | <CheckBox |
||
| 140 | style={styles.checkbox} |
||
| 141 | disabled={false} |
||
| 142 | value={accept} |
||
| 143 | color={'cornflowerblue'} |
||
| 144 | onValueChange={() => { |
||
| 145 | setAccepts((accept ? false : true)); |
||
| 146 | }} |
||
| 147 | > |
||
| 148 | |||
| 149 | </CheckBox> |
||
| 150 | |||
| 151 | <View style={styles.termsTextContainer}> |
||
| 152 | <Text style={styles.termsText}>By registering, you agree to our </Text> |
||
| 153 | <Pressable onPress={() => { |
||
| 154 | setModalVisible(!modalVisible); |
||
| 155 | setTermsVisible(true); |
||
| 156 | }}> |
||
| 157 | <Text style={[styles.termsText, {color: 'blue'}]}>terms and conditions </Text> |
||
| 158 | </Pressable> |
||
| 159 | <Text style={styles.termsText}>and </Text> |
||
| 160 | |||
| 161 | <Pressable onPress={() => { |
||
| 162 | setModalVisible(!modalVisible); |
||
| 163 | setTermsVisible(false); |
||
| 164 | }}> |
||
| 165 | <Text style={[styles.termsText, {color: 'blue'}]}>privacy policy.</Text> |
||
| 166 | </Pressable> |
||
| 167 | </View> |
||
| 168 | |||
| 169 | </View> |
||
| 170 | |||
| 171 | {accept ? |
||
| 172 | <Pressable style={styles.emailRegister} onPress={createUser}> |
||
| 173 | <Text>Register</Text> |
||
| 174 | </Pressable> |
||
| 175 | : |
||
| 176 | <Pressable style={styles.emailRegisterGray} onPress={checkAlert}> |
||
| 177 | <Text style={styles.greyedOut}>Register</Text> |
||
| 178 | </Pressable> |
||
| 179 | } |
||
| 180 | <TermsModal navigation={navigation} modalVisible={modalVisible} setModalVisible={setModalVisible} termsVisible={termsVisible}/> |
||
| 181 | <FlashMessage position={'top'}/> |
||
| 182 | </View> |
||
| 183 | ); |
||
| 263 | }); |